# This script will create a bar graph for proportion of users in clusters
# 
# Usage:
#       Rscript clusterOffset.R [inputfile] [outputdir] [outputname] [xlabel]
# 
# Where [inputfile] is a list of (label, (k proportions)) for a k-cluster model
# 
# Where [outputdir] is a directory to place the resulting graph and legend (separate files)
#
# Where [outputname] is a base name for the output files
#
# Where [xlabel] is a string for the x label
#
# The number of clusters is inferred by the script. 
#
# The plot will have a legend of cluster groups created by rainbow().

args <- commandArgs(TRUE)

inputfile <- args[1]
outputdir <- args[2]
outputname <- args[3]
xlabel <- args[4]

graphoutput <- paste(outputdir, outputname, "_plot.png", sep="")
legendoutput <- paste(outputdir, outputname, "_legend.png", sep="")

data <- as.matrix(read.table(file=inputfile, sep="\t", quote="", comment.char=""))

proportions <- t(data[, -1])
xnames <- as.character(data[, 1])
numclusters <- nrow(proportions)

clusterlabels <- as.character(c(1:numclusters))

title <- paste("Archetype User Proportion by ", xlabel, sep="")
ylabel <- "Archetype Proportion"

colors <- rainbow(numclusters)

png(graphoutput)

barplot(proportions, main=title, ylab=ylabel, xlab=xlabel, names.arg=xnames, col=colors)

dev.off()

png(legendoutput)

plot.new()

legend("topleft", legend=clusterlabels, fill=colors)

dev.off()
